package cn.ibdsr.human.manager;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 数据格式管理类
 * @author chenglx
 * @version V1.0
 * @date 2020-11-15 22:49
 * <p>
 * Date Author Description
 * ------------------------------------------------------
 * 2020-11-15 chenglx init
 */
@Component
@Slf4j
public class DataFormatManager {

    /**
     * 图片访问的前缀
     */
    @Value("${guns.fastDfs-url.visit}")
    private String fileUrlPrefix;
    /**
     * 空字符串
     */
    private static final String NULL_STR = "null";
    /**
     * HTTP协议的前缀
     */
    private static final String HTTP_SCHEME = "http";

    /**
     * 去掉图片URL中相同的前缀
     * @param fullUrl
     * @return
     */
    public String reduceFileUrl(String fullUrl) {
        if (StringUtils.isEmpty(fullUrl)) {
            return null;
        }
        if (fullUrl.startsWith(fileUrlPrefix)) {
            return fullUrl.substring(fileUrlPrefix.length());
        } else {
            log.warn("reduceUrl(): 无法精简的 fullUrl：{}", fullUrl);
            return fullUrl;
        }
    }

    /**
     * 给图片URL加上前缀，补全URL
     * @param partialUrl
     * @return
     */
    public String complementFileUrl(String partialUrl) {
        if (StringUtils.isEmpty(partialUrl)) {
            return null;
        }
        if (partialUrl.startsWith(HTTP_SCHEME)) {
            return partialUrl;
        }
        return fileUrlPrefix + partialUrl;
    }
}
